package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.pesking.core.globalid.GlobalIdManage;
import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.util.DateUtils;
import com.pesking.core.util.LogUtils;
import com.pesking.core.util.StringUtils;

public class AUTOIDSqlParameterHandler extends SqlParameterProcessor { 
		
	@Override
	public void handle(StringBuilder sbSQL,
			Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String name = sqlParameter.getName();
		String tableName = sqlParameter.getField(1);
		if (StringUtils.isBlank(tableName)){
			tableName = "default_table";
		}
		
		String date = DateUtils.date2String(new Date(), "yyyyMMddHHmmss");
		tableName += "::" + date;
		String uuid = "" + GlobalIdManage.generateGlobalID(tableName);
		if (!StringUtils.isBlank(name)){
			parameter.setParameter(name, uuid);
		}
		LogUtils.debug("AUTO_ID参数:name[" + name + "]value[" + uuid + "]");
		sbSQL.append(uuid);
	}
	
	@Override
	public int getOrder() {
		return 0;
	}


	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"AUTO_ID"});
	}
}
